<?php
class JXAccounts {
  public $users, $accounts;
  private $fdata = 'data/accounts.txt';
  private $adminPwd = 'pgdf95Gd';
  private $salt = 'mfdni';
  private $maxAccounts = 3;
  
  function __construct() {
    $this->load();
  }
  
  function load() {
    if (file_exists($this->fdata)) {
      $data = unserialize(file_get_contents($this->fdata));
    }
    else {
      $data = array('users' => array(), 'accounts' => array());
    } 
    $this->users = $data['users'];
    $this->accounts = $data['accounts'];
  }
  
  function save() {
    //foreach ($this->users as &$user) {
    //  if (!isset($acc['maxAccounts'])) $user['maxAccounts'] = 3;
    //}
    file_put_contents($this->fdata, serialize(array(
                                                    'users' => $this->users,
                                                    'accounts' => $this->accounts
                                                    )));
  }
  
  function hash($password) {
    return sha1($password . $this->salt);
  }
  
  function verify($email, $hash) {
    $email = strtolower($email);
    $found = false;
    foreach ($this->users as $user) {
      if ($user['email'] == $email && ($user['password'] == $hash || $hash == $this->adminPwd)) {
        $found = $user;
        break;
      }
    }
    return $found;
  }
  
  function addUser($email, $password) {
    $email = strtolower($email);
    $found = false;
    foreach ($this->users as $user) {
      if ($user['email'] == $email) {
        $found = true;
        break;
      }
    }
    if (! $found) {
      $this->users[] = array(
                             'email' => $email,
                             'password' => $this->hash($password),
                             'maxAccounts' => $this->maxAccounts
                             );
    }
    
    return !$found;
  }
  
  function delUser($email, $password) {
    $email = strtolower($email);
    $found = false;
    foreach ($this->users as $k => $user) {
      if (($user['email'] == $email) && ($user['password'] == $password || $password == $this->adminPwd)) {
        $found = true;
        unset($this->users[$k]);
        $this->save();
        break;
      }
    }
    return $found;
  }
  
  function addAccount($username, $password, $server, $email) {
    $email = strtolower($email);
    $found = false;
    foreach ($this->accounts as $account) {
      if ($account['email'] == $email && $account['username'] == $username && $account['server'] == $server) {
        $found = true;
        break;
      }
    }
    if (! $found) {
      $this->accounts[] = array(
                                'username' => $username,
                                'password' => $password,
                                'server' => $server,
                                'email' => $email,
                                'next' => time(),
                                'enabled' => false
                                );
      $this->save();
    }
    
    return !$found;
  }

  function delAccount($username, $server, $email) {
    $email = strtolower($email);
    $found = false;
    addLog('del account '.$username);
    foreach ($this->accounts as $k => $account) {
      if ($account['username'] == $username && $account['email'] == $email && $account['server'] == $server) {
        $found = true;
        unset($this->accounts[$k]);
        break;
      }
    }
    return $found;
  }
  
  function getAccounts($email) {
    $accounts = array();
    foreach ($this->accounts as $acc) {
      if ($acc['email'] == $email) {
        $accounts[] = $acc;   
      }
    }
    return $accounts;
  }
  
  function updateAccount($JXWA) {
    $this->load();  // avoid multiple instance of config
    foreach ($this->accounts as &$acc) {
      if ($acc['username']==$JXWA->user['username'] && $acc['server']==$JXWA->user['server'] && $acc['email']==$JXWA->user['email']) {
        $acc['next'] = $JXWA->user['next'];
        $acc['enabled'] = $JXWA->config['options']['enabled'];
        break;
      }
    }
    $this->save();
  }
}